package com.frame.filter

import javax.ws.rs.container.ContainerRequestContext
import javax.ws.rs.container.ContainerResponseContext
import javax.ws.rs.container.ContainerResponseFilter
import javax.ws.rs.core.MediaType

/**
 * Created with IntelliJ IDEA.
 * com.myTest
 * User: 张斌
 * Date: 2014/7/23
 */
class CharsetResponseFilter implements ContainerResponseFilter{

    @Override
    public void filter(ContainerRequestContext request, ContainerResponseContext response) {
        MediaType type = response.getMediaType();

        println "charset = ${type.toString()}"
        if (type != null) {
            String contentType = type.toString();
            if (!contentType.contains("charset")) {
                contentType = contentType + ";charset=utf-8";
                response.getHeaders().putSingle("Content-Type", contentType);
            }
        }
    }

}
